/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stendcontrol;

/**
 *
 * @author minaev
 */
public class SensorInformer implements TransmitListener {

    final int cnt_sensors = Configurator.COUNT_SENSOR;
    int sensors[] = new int[cnt_sensors];
    int coef0[] = new int[cnt_sensors];
    double coef_prop[] = new double[cnt_sensors];

    public SensorInformer(int[] coef0, double[] coef_prop) {
        for (int i = 0; i < cnt_sensors; i++) {
            this.sensors[i] = -1;
            this.coef0[i] = 0;
            this.coef_prop[i] = 1;
        }
        if (coef0.length == cnt_sensors && coef_prop.length == cnt_sensors) {
            this.coef0 = coef0;
            this.coef_prop = coef_prop;
        } else {
            StendLogger.log(StendLogger.MSG_ERROR, "Bad lenth of coef0(len:"
                    + coef0.length + ") and/or coef_prop(len:"
                    + coef_prop.length + ") in SensorInformer. Need lenth:" + cnt_sensors
                    + ". Loaded default coef0 and coef_prop");
        }
    }

    public synchronized int getSensor(int nsensor) {
        if (nsensor >= 0 && nsensor < cnt_sensors) {
            return sensors[nsensor];
        } else {
            return -1;
        }
    }

    public double getScaleSensor(int nsensor) {
        if (nsensor >= 0 && nsensor < cnt_sensors) {
            return SclVal.scale(getSensor(nsensor), coef0[nsensor],
                    coef_prop[nsensor]);
        } else {
            StendLogger.log(StendLogger.MSG_ERROR, "Bad index sensor for"
                    + " getScaleSensor in SensorInformer nsensor:" + nsensor);
            return -1;
        }
    }

    @Override
    public synchronized void transmitPerformed(byte[] buf, int len) {
        if (buf[0] >> 4 == AbstractMessage.MSG_MSEQ) {
            if (len >= 1 + cnt_sensors * 2) {
                for (int i = 0; i < cnt_sensors; i++) {
                    sensors[i] = DataExtract.getSensor(buf, len, i);
                }
            } else {
                StendLogger.log(StendLogger.MSG_ERROR, "Small buffer for"
                        + " SensorInformer. Min need len:"
                        + (1 + cnt_sensors * 2) + " This len:" + len);
            }
        }
    }
}
